package com.mui.controls
{
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	public class Alert extends Object
	{
		public static const YES:int = 1;
		public static const NO:int = 2;
		public static const OK:int = 4;
		public static const CANCEL:int = 8;
		public static const Mode_Blur:String = "blur";
		public static const Mode_None:String = "none";
		public static const Mode_NoneNotModal:String = "nonenotmodal";
		public static const Mode_Simple:String = "simple";
		public static var okLabel:String = "确定";
		public static var calcelLabel:String = "取消";
		public static var yesLabel:String = "是";
		public static var noLabel:String = "否";
		public static var text:String = "";
		public static var title:Object;
		public static var buttonFlags:uint = 0;
		public static var parent:DisplayObjectContainer;
		public static var closeHandler:Function;
		public static var iconClass:Class;
		public static var defaultButtonFlag:uint = 4;
		public static var buttonWidth:int = 49;
		public static var buttonHeight:int = 25;
		public static var showCloseBtn:Boolean;
		public static var textColor:uint = 16777215;
		public static var titleColor:uint = 16777215;
		public static var buttonColor:uint = 5143985;
		public static var buttonTextColor:uint = 16777215;
		public static var backgroundColor:uint = 5143985;
		public static var backgroundAlpha:Number = 0;
		public static var buttonStyleName:String;
		public static var titleTextFormat:TextFormat;
		public static var messageTextFormat:TextFormat;
		public static var mode:String = "simple";
		public static var alertWinRenderer:Class = null;
		public static var timerOut:int = 0;
		public static var timerNO:Boolean = false;
		public static var extendObj:Object;
		private static var _defaultAlertWinRenderer:Class = null;
		private static var stage:Object = null;
		private static var _instance:Alert;
		
		public function Alert(container:DisplayObjectContainer)
		{
			if (_instance != null)
			{
				throw new Error(" AlertManager 单例 ");
			}
			stage = container;
		}
		
		public static function get defaultAlertWinRenderer() : Class
		{
			return _defaultAlertWinRenderer;
		}
		
		public static function set defaultAlertWinRenderer(defaultClass:Class) : void
		{
			alertWinRenderer = defaultClass;
			_defaultAlertWinRenderer = defaultClass;
		}
		
		public static function init(container:DisplayObjectContainer) : void
		{
			if (_instance == null)
			{
				_instance = new Alert(container);
			}
		}
		
		public static function show(text:String = "", title:* = null, flags:uint = 4, parent:Sprite = null, closeHandler:Function = null, iconClass:Class = null, defaultButtonFlag:uint = 4) : Sprite
		{
			var myAlert:Sprite;
			var container:DisplayObjectContainer;
			var containerWidth:Number;
			var containerHeight:Number;
			var clickHandler:Function;
			var removeHandler:Function;
			clickHandler = function (event:MouseEvent) : void
			{
				var displayObject:* = event.target as DisplayObject;
				var gAlertWin:* = event.currentTarget as GAlertWin;
				if (!gAlertWin.window.contains(displayObject))
				{
					if (gAlertWin.promptBackground)
					{
						flick(gAlertWin.promptBackground);
					}
				}
				
			};
			removeHandler = function (event:Event) : void
			{
				myAlert.removeEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
				myAlert.removeEventListener(MouseEvent.CLICK, clickHandler);
				
			};
			Alert.text = text;
			Alert.title = title;
			Alert.buttonFlags = flags;
			if (parent)
			{
				Alert.parent = parent;
			}
			Alert.closeHandler = closeHandler;
			if (iconClass)
			{
				Alert.iconClass = iconClass;
			}
			Alert.defaultButtonFlag = defaultButtonFlag;
			if (!stage && !parent)
			{
				throw new Error("AlertManager尚未初始化!必需指定Alert窗口显示的容器");
			}
			if (parent)
			{
				containerWidth = parent.width;
				containerHeight = parent.height;
				container = parent;
			}
			else
			{
				if (stage is Stage)
				{
					containerWidth = stage.stageWidth;
					containerHeight = stage.stageHeight;
				}
				else if (stage is DisplayObjectContainer)
				{
					containerWidth = stage.stage.stageWidth;
					containerHeight = stage.stage.stageHeight;
				}
				container = stage as DisplayObjectContainer;
			}
			if (alertWinRenderer)
			{
				myAlert = new alertWinRenderer(containerWidth, containerHeight);
			}
			else
			{
				myAlert = new GAlertWin(stage.stageWidth, stage.stageHeight);
			}
			container.addChild(myAlert);
			if (myAlert is GAlertWin)
			{
				myAlert.addEventListener(MouseEvent.CLICK, clickHandler);
				myAlert.addEventListener(Event.REMOVED_FROM_STAGE, removeHandler);
			}
			resetToDefault();
			return myAlert;
		}
		
		public static function flick(obj:DisplayObject) : void
		{
			var timeLite:TimelineLite;
			var x:* = obj.x;
			var y:* = obj.y;
			timeLite = new TimelineLite();
			timeLite.append(new TweenLite(obj, 0.05, {glowFilter:{color:14540253, alpha:0.5, blurX:100, blurY:100, inner:true}}));
			timeLite.append(new TweenLite(obj, 0.05, {glowFilter:{color:14540253, alpha:1, blurX:1, blurY:1, inner:true}}));
			timeLite.append(new TweenLite(obj, 0.05, {glowFilter:{color:14540253, alpha:0.5, blurX:100, blurY:100, inner:true}}));
			timeLite.append(new TweenLite(obj, 0.05, {glowFilter:{color:14540253, alpha:1, blurX:1, blurY:1, inner:true}}));
			timeLite.append(new TweenLite(obj, 0.05, {glowFilter:{color:14540253, alpha:0.5, blurX:100, blurY:100, inner:true}}));
			timeLite.append(new TweenLite(obj, 0.05, {glowFilter:{color:14540253, alpha:1, blurX:1, blurY:1, inner:true}, 
				onComplete:function () : void
				{
					timeLite.stop();
					timeLite.kill();
					timeLite = null;
				}}));
			timeLite.play();
		}
		
		public static function resetToDefault() : void
		{
			okLabel = "确定";
			calcelLabel = "取消";
			yesLabel = "是";
			noLabel = "否";
			text = "";
			title = null;
			buttonFlags = 0;
			parent = null;
			closeHandler = null;
			iconClass = null;
			defaultButtonFlag = 4;
			buttonWidth = 49;
			buttonHeight = 25;
			textColor = 16777215;
			titleColor = 16777215;
			buttonColor = 5143985;
			buttonTextColor = 16777215;
			backgroundColor = 5143985;
			backgroundAlpha = 0;
			titleTextFormat = null;
			messageTextFormat = null;
			alertWinRenderer = defaultAlertWinRenderer;
			mode = "simple";
			timerOut = 0;
			buttonStyleName = null;
			showCloseBtn = false;
			timerNO = false;
			extendObj = null;
		}
	}
}
